package com.example.demo.controller;

import com.example.demo.entity.ResultEntity;
import com.example.demo.service.serviceimpl.BannersServiceImpl;
import com.example.demo.vo.BannersVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/banners/")
@CrossOrigin
@RestController
public class BannersController {
    @Resource
    BannersServiceImpl bannersService;
    
    // 获取所有轮播图列表
    @GetMapping("getBanners")
    private ResultEntity getBanners() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(bannersService.getBanners());
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("轮播图获取失败");
            e.printStackTrace();
        }
        return resultEntity;
    }
    
    // 添加轮播图
    @PostMapping("addBanners")
    private ResultEntity addBanners(@RequestBody BannersVo bannersVo) {
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(bannersService.addBanners(bannersVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功");
            }else{
                resultEntity.setCode(-1);
                resultEntity.setMsg("添加失败");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
    
    // 更新轮播图
    @PutMapping("updateBanners")
    private ResultEntity updateBanners(@RequestBody BannersVo bannersVo) {
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(bannersService.updateBanners(bannersVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功");
            }else{
                resultEntity.setCode(-1);
                resultEntity.setMsg("修改失败");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
    
    // 删除轮播图
    @DeleteMapping("deleteBanners")
    private ResultEntity deleteBanners(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(bannersService.deleteBanners(id)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功");
            }else{
                resultEntity.setCode(-1);
                resultEntity.setMsg("删除失败");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
}
